<?php
namespace app\admin\controller;

class Index extends Common
{
    public function index()
    {
        return view();
    }
    public function main()
    {
      
       
    }

    public function list_to_tree($list, $pk='id', $pid = 'pid', $child = 'child', $root = 0) {
        // 创建Tree
        $tree = array();
        if(is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
             
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId =  $data[$pid];
                if ($root == $parentId) {
                    $tree[] =& $list[$key];
                }else{
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }
    public function menu(){
        $menu = (new \app\common\model\Menu())
        ->where(['is_hidden' => 1])
        ->order('sort asc,id asc')
        ->select()->toArray();
        $menu = $this->list_to_tree($menu);
        $data = [
            "homeInfo" => [
                "title" => "首页",
                "href" => url('Index/main')
            ],
            "logoInfo" => [
                "title" => config('app.app_name'),
                "image" => "/logo-ie.png",
                "href" => url('Index/index')
            ],
            "menuInfo" => $menu
        ];
     
        return json($data);
    }
 
}
